package com.ruoyi.crm.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.MongoLog;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.crm.domain.Clue;
import com.ruoyi.crm.domain.vo.ClueTrackRecordVo;
import com.ruoyi.crm.domain.vo.RestoreClueVo;
import com.ruoyi.crm.service.IClueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/crm/clue/track")
public class ClueTrackRecordController extends BaseController {
    @Autowired
    private IClueService clueService;

    /**
     * 新增线索跟进记录
     */
    @PreAuthorize("@ss.hasPermi('crm:clue:edit')")
    @Log(title = "线索跟进记录", businessType = BusinessType.INSERT)
    @MongoLog(tableName = MongoLog.TableName.CLUE, title = "线索跟进记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult record(@RequestBody ClueTrackRecordVo clueTrackRecordVo) {
        clueTrackRecordVo.setStatus(Clue.Status.TRACKING.getValue());
        return toAjax(clueService.updateClue(clueTrackRecordVo));
    }
    /**
     * 转伪线索
     */
    @PreAuthorize("@ss.hasPermi('crm:clue:edit')")
    @Log(title = "线索跟进记录", businessType = BusinessType.UPDATE)
    @MongoLog(tableName = MongoLog.TableName.CLUE, title = "线索跟进记录", businessType = BusinessType.UPDATE)
    @PutMapping("/fake/{id}")
    public AjaxResult fake(@RequestBody ClueTrackRecordVo clueTrackRecordVo) {
        return toAjax(clueService.fakeClue(clueTrackRecordVo.getClueId()));
    }
    /**
     * 捞取
     */
    @PreAuthorize("@ss.hasPermi('crm:clue:edit')")
    @Log(title = "线索跟进记录", businessType = BusinessType.UPDATE)
    @MongoLog(tableName = MongoLog.TableName.CLUE, title = "线索跟进记录", businessType = BusinessType.UPDATE)
    @PutMapping("/restore/{id}")
    public AjaxResult restore(@RequestBody RestoreClueVo restoreClueVo)
    {
        return toAjax(clueService.restoreClue(restoreClueVo.getIds()));
    }

    /**
     * 转商机
     */
    @PreAuthorize("@ss.hasPermi('crm:clue:edit')")
    @Log(title = "线索跟进记录", businessType = BusinessType.UPDATE)
    @MongoLog(tableName = MongoLog.TableName.CLUE, title = "线索跟进记录", businessType = BusinessType.UPDATE)
    @PutMapping("/transform/{id}")
    public AjaxResult transform(@RequestBody Map<String, String> body)
    {
        return toAjax(clueService.transform2Business(Long.valueOf(body.get("clueId"))));
    }
}
